/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.i.kotionokyurievna.jewelryweb.entities;

import java.io.Serializable;
import javax.persistence.*;

/**
 *
 * @author Kateryna_Reznik
 */
@Entity
@Table (name="users")
@NamedQueries({
    @NamedQuery ( name = "User.findById",
        query="SELECT u.userId, u.login, u.password, u.userRole "
        + "FROM User u "
        + "WHERE u.userId = :userId"),
   @NamedQuery ( name = "User.findByLogin",
        query="SELECT u "
        + "FROM User u "
        + "WHERE u.login = :login"),
    @NamedQuery ( name = "User.findByRole",
        query="SELECT u.userId, u.login, u.password, u.userRole "
        + "FROM User u "
        + "WHERE u.userRole = :userRole"),
    @NamedQuery ( name = "User.findAll",
        query="SELECT u.userId, u.login, u.password, u.userRole "
        + "FROM User u ")
})
@Inheritance(strategy = InheritanceType.JOINED)
public class User implements Serializable{
    public static final String FIND_BY_ID = "User.findById";
    public static final String FIND_BY_LOGIN = "User.findByLogin";
    public static final String FIND_BY_ROLE = "User.findByRole";
    @Id
    @GeneratedValue (strategy = GenerationType.TABLE)
    @Column (name = "user_id")
    private int userId;
    private String login;
    private String password;
    @Enumerated (EnumType.STRING)
    @Column(name ="user_role")
    private UserRole userRole;
    
    public User(){}
    
    public User( String login, String password){
        this.login = login;
        this.password = password; 
        this.userRole = UserRole.EMPLOYEE;
    }
    
    public User( String login, String password, UserRole userRole){
        this.login = login;
        this.password = password; 
        this.userRole = userRole;
    }
    
    public User( int id, String login, String password){
        this.userId = id;
        this.login = login;
        this.password = password;
        this.userRole = UserRole.EMPLOYEE;
    }
    
    public User( int id, String login, String password, UserRole userRole){
        this.userId = id;
        this.login = login;
        this.password = password;
        this.userRole = userRole;
    }

    public User( User another ) {
        if ( another != null ){
            this.userId = another.userId;
            this.login = another.login;
            this.password = another.password;
            this.userRole = another.userRole;
        }
    }
    
    public int getUserId() {
        return userId;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }
    
    
    public boolean isLegalUser( String userPassword ){
        if ( userPassword != null && password != null ){
            if (userPassword.equals(password)){
                return true;
            }
        } 
        return false;
    }
    
    public boolean setPassword( String oldPassword, String newPassword ){
        if ( password.isEmpty() ){
            password = newPassword.substring(0);
            return true;
        }
        else{
            if ( !oldPassword.isEmpty() && (oldPassword.equals(password)) ) {
                password = newPassword.substring(0);
                return true;
            }
        }
        return false;
    }

    public UserRole getRole() {
        return userRole;
    }

    public void setRole(UserRole role) {
        this.userRole = role;
    }

    @Override
    public String toString() {
        return "User{" + "login=" + login + '}';
    }
    
    

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 61 * hash + (this.login != null ? this.login.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final User other = (User) obj;
        if ((this.login == null) ? (other.login != null) : !this.login.equals(other.login)) {
            return false;
        }
        return true;
    }
    
    
}
